package com.dingwen.rabcon.service.impl;

import com.dingwen.rabcon.constant.RabbitConstant;
import com.dingwen.rabcon.service.SmsService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * sms service impl <br>
 * <pre>
 *     1. {@code @RabbitListener} 可在类上使用也可在方法上使用
 *     2. {@code @RabbitHandler} 配合 {@code @RabbitListener} 在方法上面使用，按照指定消息类型进行转换
 * </pre>
 *
 * @author dingwen
 * 2021.06.17 11:23
 */
@Service
@RabbitListener(queues = {RabbitConstant.FANOUT_QUEUE_SMS_NAME}, concurrency = "4", ackMode = "MANUAL")
public class SmsServiceImpl implements SmsService {
    @Override
//    @RabbitHandler
    public void sendMessage(String message) {
        System.out.println("S: message = " + message);
    }

    @RabbitHandler
    public void testSendMessage(Message messages, String message, Channel channel) throws IOException {
        // 消息ID
        long deliveryTag = messages.getMessageProperties().getDeliveryTag();

        System.out.println("message = " + message);

        channel.basicAck(deliveryTag, Boolean.FALSE);

        // 确认消费失败，单条确定，重回队列
//        channel.basicNack(deliveryTag, Boolean.FALSE, Boolean.TRUE);
    }
}
